﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cecbData;

namespace cecbClasses.Data_Adapters
{
    public  class SimpleMatrixCalculation
    {

        public SimpleMatrixCalculation()
        {
            cecbContext = new DSSEntities();
        }
        DSSEntities cecbContext;


        double sum;


        // calculate the Physical Environmental Affection
        public double getPhysicalEnvironmentalAffection(string project)
        {
            double phisicalImpact = 0;


            var query = cecbContext.ExecuteStoreQuery<string>
            ("select SimpleMatrixValues.actv_reference from SimpleMatrixValues where SimpleMatrixValues.proj_reference = '" + project + "'  ");
            foreach (var activity in query)
            {


                var count1 = (from c in cecbContext.SimpleMatrixValues
                              join b in cecbContext.Impacts on c.impt_reference equals b.impt_reference
                              join g in cecbContext.Components on b.impt_component equals g.cmpt_reference
                              where c.proj_reference.Contains(project)
                              && g.cmpt_mainCat.Contains("Physical Environmental") && c.proj_reference.Contains(project)

                              group c by new
                              {
                                  c.proj_reference
                              }

                                  into cc
                                  select cc.Sum(c => c.mtrx_value) / sum).FirstOrDefault();

                phisicalImpact = count1.Value;

            }

            phisicalImpact = Math.Round(phisicalImpact, 2);

            return phisicalImpact;
        }


        // calculate the Physical Environmental Affection as a presentage
        public double getPhysicalEnvironmentalAffectionPresentage(string project)
        {
            double presentage = 0;

            var query = cecbContext.ExecuteStoreQuery<string>
            ("select SimpleMatrixValues.actv_reference from SimpleMatrixValues where SimpleMatrixValues.proj_reference = '" + project + "'  ");
            foreach (var activity in query)
            {


                var quary = (from c in cecbContext.SimpleMatrixValues
                             join b in cecbContext.Impacts on c.impt_reference equals b.impt_reference
                             join g in cecbContext.Components on b.impt_component equals g.cmpt_reference
                             where c.proj_reference.Contains(project)
                             && g.cmpt_mainCat.Contains("Physical Environmental")

                             group c by new
                             {
                                 c.proj_reference
                             }

                                 into cc
                                 select cc.Sum(c => c.mtrx_value) / sum).FirstOrDefault();

                presentage = quary.Value;

            }
            presentage = Math.Round(presentage, 2) * 100;

            return presentage;
        }

        // calculate the Echological Environmental Affection
        public double getEchologicallyAffectin(string project)
        {
            double isCorrect = 0;
            double echologicalAffection = 0;
            try
            {

                var quary = (from c in cecbContext.SimpleMatrixValues
                             join b in cecbContext.Impacts on c.impt_reference equals b.impt_reference
                             join g in cecbContext.Components on b.impt_component equals g.cmpt_reference
                             where c.proj_reference.Contains(project)
                             && g.cmpt_mainCat.Contains("Ecological")

                             group c by new
                             {
                                 c.proj_reference
                             }

                                 into cc
                                 select cc.Sum(c => c.mtrx_value) / sum).FirstOrDefault();

                echologicalAffection = quary.Value;
            }
            catch (Exception)
            {

                isCorrect = -1;
            }
            echologicalAffection = Math.Round(echologicalAffection, 2);
            return echologicalAffection;
        }
        // calculate the Echological Environmental Affection as a presentage
        public double getEchologicallyAffectinPresentage(string project)
        {
            double isCorrect = 0;
            double presentage = 0;

            try
            {
                var quary = (from c in cecbContext.SimpleMatrixValues
                             join b in cecbContext.Impacts on c.impt_reference equals b.impt_reference
                             join g in cecbContext.Components on b.impt_component equals g.cmpt_reference
                             where c.proj_reference.Contains(project)
                             && g.cmpt_mainCat.Contains("Ecological")

                             group c by new
                             {
                                 c.proj_reference
                             }

                                 into cc
                                 select cc.Sum(c => c.mtrx_value) / sum).FirstOrDefault();

                presentage = quary.Value;
            }
            catch (Exception)
            {
                isCorrect = -1;
            }
            presentage = Math.Round(presentage, 2) * 100;

            return presentage;
        }
        // calculate the SociologicallyAffection as aPresentage
        public double getSociologicallyAffectionPresentage(string project)
        {
            double presentage = 0;
            double isCorrect = 0;

            try
            {
                var quary = (from c in cecbContext.SimpleMatrixValues
                             join b in cecbContext.Impacts on c.impt_reference equals b.impt_reference
                             join g in cecbContext.Components on b.impt_component equals g.cmpt_reference
                             where c.proj_reference.Contains(project)
                             && g.cmpt_mainCat.Contains("Socio Environmental")

                             group c by new
                             {
                                 c.proj_reference
                             }

                                 into cc
                                 select cc.Sum(c => c.mtrx_value) / sum).FirstOrDefault();

                presentage = quary.Value;
            }
            catch (Exception)
            {
                isCorrect = -1;

            }
            presentage = Math.Round(presentage, 2) * 100;

            return presentage;
        }

        // calculate the Sociological Environmental Affection 
        public double getSociologicallyAffection(string project)
        {
            double isCorrect = 0;
            double sociologicalAffection = 0;
            try
            {

                var count1 = (from c in cecbContext.SimpleMatrixValues
                              join b in cecbContext.Impacts on c.impt_reference equals b.impt_reference
                              join g in cecbContext.Components on b.impt_component equals g.cmpt_reference
                              where c.proj_reference.Contains(project)
                              && g.cmpt_mainCat.Contains("Socio Environmental")

                              group c by new
                              {
                                  c.proj_reference
                              }

                                  into cc
                                  select cc.Sum(c => c.mtrx_value) / sum).FirstOrDefault();

                sociologicalAffection = count1.Value;
            }
            catch (Exception)
            {

                isCorrect = -1;
            }
            sociologicalAffection = Math.Round(sociologicalAffection, 2);
            return sociologicalAffection;
        }

        // calculate the Summation of the total Affection
        public double getSummation(string project)
        {
            try
            {
                var quary = (from c in cecbContext.SimpleMatrixValues

                             where c.proj_reference.Contains(project)



                             group c by new
                             {
                                 c.proj_reference
                             }
                                 into cc
                                 select cc.Sum(c => c.mtrx_value)).FirstOrDefault();
                sum = quary.Value;
            }
            catch (Exception)
            {


            }

            return sum;
        }

        // check the correctivity of the result
        public double CheckCorrectivity(string project)
        {
            double isCorrect = 0;
            double result = 0;
            try
            {
                double summation = getSummation(project);
                var quary = (from c in cecbContext.SimpleMatrixValues
                             join b in cecbContext.Impacts on c.impt_reference equals b.impt_reference
                             join g in cecbContext.Components on b.impt_component equals g.cmpt_reference
                             where c.proj_reference.Contains(project)


                             group c by new
                             {
                                 c.proj_reference
                             }

                                 into cc
                                 select cc.Sum(c => c.mtrx_value) / summation).FirstOrDefault();

                result = quary.Value;
            }
            catch (Exception)
            {
                result = -1;
            }
            return result;

        }

        public bool InsertValuesToSimpleMatrix(String impR, String actR, String proR, String impV)
        {
            bool IsInserted = false;
            try
            {
                cecbData.SimpleMatrixValues sim = new SimpleMatrixValues();
                sim.proj_reference = proR;
                sim.impt_reference = impR;
                sim.actv_reference = actR;
                sim.mtrx_value = int.Parse(impV);
                sim.mtrx_isRelated = true;
                sim.mtrx_status = true;
                cecbContext.AddToSimpleMatrixValues(sim);
                cecbContext.SaveChanges();
                IsInserted = true;
            }
            catch (Exception)
            {

                IsInserted = false;
            }


            return IsInserted;
        }

       
    }
}
